package com.strongbj.iot.devices.amazonNewReader.request.handle;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.strongbj.core.message.IMessageHandle;
import com.strongbj.core.util.ByteUtil;
import com.strongbj.iot.devices.amazonNewReader.message.AmazonNewReaderOSMessageFactory;
import com.strongbj.iot.devices.amazonNewReader.message.MQMessageOfAmazonNewReader;
import com.strongbj.iot.devices.amazonNewReader.request.entity.AmazonNewReaderCommenEntity;
import com.strongbj.iot.devices.amazonNewReader.request.entity.T;
import com.strongbj.iot.devices.amazonNewReader.response.handle.NewReaderResponseHandleContext;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
/**
 * 标签盘点，针对多个标签，最大数量为 50
 * @author 25969
 *
 */
public class LedInventoryForManyHandle implements IMessageHandle<MQMessageOfAmazonNewReader,Object>{
	private static Logger logger = LogManager.getLogger(LedInventoryForManyHandle.class.getName());
	private final static String ACTION_CODE = "ledInventoryForMany";   //MQ中判断消息类型的标识符
	private final static byte command = (byte)0x08;
	private AmazonNewReaderOSMessageFactory amazonNewReaderOSMessageFactory = new AmazonNewReaderOSMessageFactory();
//	private static SimpleDateFormat df=null;
	@Override
	public boolean isHandle(MQMessageOfAmazonNewReader t) {
		if(t.getActioncode().equals(ACTION_CODE)){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Object handle(ChannelHandlerContext ctx, MQMessageOfAmazonNewReader t) {
		logger.info("AmazonNewReader 收到MQ发来的“标签盘点，针对多个标签，最大数量为 50” json={}",JSON.toJSONString(t));
		AmazonNewReaderCommenEntity amazonNewReaderCommen = ((JSONObject)t.getAwsPostdata()).toJavaObject(AmazonNewReaderCommenEntity.class);		
		 this.messageDataToBytes(amazonNewReaderCommen);  
		return null;
	}

	
	private void messageDataToBytes(AmazonNewReaderCommenEntity yy) {
		String readerCode = yy.getReaderCode();  //reader地址码
		String shortAddress = yy.getShortAddress();
		int legNum = yy.getLegNum();
		int hour = yy.getHour();
		int minute = yy.getMinute();
		int second = yy.getSecond();
		int millisecond = yy.getMillisecond();
		byte[] sendData = new byte[4];   
		byte[] dest = new byte[3];   //接受主机编号的临时byte数组
		ByteUtil.hexStringToBytes(shortAddress,sendData,0);
		 ByteUtil.shortToBytes((short)legNum, sendData,2);
		//sendData[2]=(byte)legNum;
		byte[] timeStamp = new byte[4];
		StringBuffer time = new StringBuffer();
		String hourByte = T.getHourByte(hour);
		String minuteByte = T.getMinuteByte(minute);
		String secondByte = T.getSecondByte(second);
		String millisecondByte = T.getMillisecond(millisecond);	
		time.append(hourByte).append(minuteByte).append(secondByte).append(millisecondByte).toString();
		timeStamp[0] = Integer.valueOf(time.toString().substring(0, 8),2).byteValue();
		timeStamp[1] = Integer.valueOf(time.toString().substring(8, 16),2).byteValue();
		timeStamp[2] = Integer.valueOf(time.toString().substring(16, 24),2).byteValue();
		timeStamp[3] = Integer.valueOf(time.toString().substring(32-8),2).byteValue();
		byte[] datas = amazonNewReaderOSMessageFactory.createAmazonNewReaderOSMessage(ByteUtil.hexStringToBytes(readerCode,dest,0), command, sendData,timeStamp);
		ByteBuf bs  = Unpooled.copiedBuffer(datas);
		NewReaderResponseHandleContext.channels.writeAndFlush(bs);
		logger.info("AmazonNewReader 发送[标签盘点，针对多个标签，最大数量为 50]命令成功 message={}",ByteUtil.byteArrToHexString(datas));
	}
}
